#ifndef ROBOTSCRIPTI_H
#define ROBOTSCRIPTI_H
#include <Ice/Ice.h>
#include "RobotDef.h"
#include <QtCore>
#include <QThread>
#include <QScriptEngine>

class TmJsEngineWorker;
class TmJsEngineCtrl;

class TmJsEngineState : public QObject
{
    Q_OBJECT
    Q_PROPERTY(int nFrameID READ getFrameID WRITE setFrameID /*NOTIFY nFrameIDChanged*/)
    Q_PROPERTY(int nFrameMSleep READ getFrameDelay WRITE setFrameDelay /*NOTIFY bStoppedChanged*/)
    Q_PROPERTY(bool bStopped READ getStopped WRITE setStopped /*NOTIFY bStoppedChanged*/)
    Q_PROPERTY(bool bIsModule READ getIsModule) // only by read
public:
    explicit TmJsEngineState(bool bIsModule=true, QObject *parent = 0);

    void incFrameID();
    void reset();
private:
    int pr_nFrameID;
    int pr_nFrameDelay;
    bool pr_bStopped;
    //-----
    bool pr_bIsModule;
signals:

public slots: // Property
    int getFrameID() const;
    void setFrameID(int nFrameID);
    int getFrameDelay() const;
    void setFrameDelay(int nFrameDelay);
    bool getStopped() const;
    void setStopped(bool bStopped);
    bool getIsModule() const;
};

class TmJsEngineCtrl : public QObject
{
    Q_OBJECT
public:
    explicit TmJsEngineCtrl(QObject *parent = 0);
    void init();
    void unInit();
    void js_main_load_file(QString strFilePath);
    void js_main_load_content(QString strJsContent);
    void js_main_stop();
    bool js_main_isPlaying();

private:
    TmJsEngineState* pr_pEngineState;
    TmJsEngineWorker* pr_pEngineWorker;
signals:

public slots:
};

class TmJsEngineWorker : public QThread
{
    Q_OBJECT
public:
    explicit TmJsEngineWorker(TmJsEngineState* pWork, QObject *parent = 0);
    void run();
    void load_js_start(QString strJsContent);
    void msg_js_event(QString event, QString val);

    bool pr_bExit;
private:
    bool pr_bStartLoad;
    QString pr_strJsContent;
    QScriptEngine* pr_pEngine;
    TmJsEngineState* pr_pWork;
    QScriptValue pr_func_event;
signals:

public slots:
};

class RobotScriptI : public Robot::Script
{
public:
    RobotScriptI();
};

#endif // ROBOTSCRIPTI_H
